/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerStarter.java
 *
 * Created on 23.08.2009, 02:51:34
 */

package Game;

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

/**
 *
 * @author Patrice
 */
public class ServerInitFrame extends javax.swing.JFrame {

    Server server;
    boolean maiden = true;
    private final Color LIGHT_RED = new Color(255, 140, 155);
    private Thread player_counter;
    private String IP;

    /** Creates new form ServerStarter */
    public ServerInitFrame() {
        initComponents();
        try {
            IP = InetAddress.getLocalHost().toString().split("/")[1];
            int index_p1 = IP.indexOf(".");
            int index_p2 = index_p1+(IP.substring(index_p1+1).indexOf("."))+1;
            int index_p3 = IP.lastIndexOf(".");
            textfield_IP_1.setText(IP.substring(0, index_p1));
            textfield_IP_2.setText(IP.substring(index_p1+1, index_p2));
            textfield_IP_3.setText(IP.substring(index_p2+1, index_p3));
            textfield_IP_4.setText(IP.substring(index_p3+1, IP.length()));
        }
        catch (IOException ex){}

        this.setLocation(MouseInfo.getPointerInfo().getLocation());

        player_counter =  new Thread(){

            @Override
            public void run() {
                while (true){
                    label.setText(String.valueOf(server.getUserCount()));
                    try {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException ex){
                        ex.printStackTrace();
                    }
                }
            }
        };
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        textfield_port = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        button_default_port = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        textfield_users = new javax.swing.JTextField();
        button_users = new javax.swing.JButton();
        button_start = new javax.swing.JButton();
        button_connect = new javax.swing.JButton();
        button_dispose = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        label = new javax.swing.JLabel();
        textfield_IP_4 = new javax.swing.JTextField();
        textfield_IP_2 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        textfield_IP_1 = new javax.swing.JTextField();
        textfield_IP_3 = new javax.swing.JTextField();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Server");
        setAlwaysOnTop(true);
        setResizable(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel4.setText("ServerSettings");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Port:");

        button_default_port.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_default_port.setText("Default");
        button_default_port.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_default_portActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Number of players");

        button_users.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_users.setText("unlimited");
        button_users.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_usersActionPerformed(evt);
            }
        });

        button_start.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_start.setText("Start");
        button_start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_startActionPerformed(evt);
            }
        });

        button_connect.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_connect.setText("Connect");
        button_connect.setEnabled(false);
        button_connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_connectActionPerformed(evt);
            }
        });

        button_dispose.setFont(new java.awt.Font("Tahoma", 0, 10));
        button_dispose.setText("Dispose");
        button_dispose.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(204, 0, 0), 2, true));
        button_dispose.setEnabled(false);
        button_dispose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_disposeActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("IP:");

        label.setFont(new java.awt.Font("Tahoma", 0, 14));
        label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label.setText("0");
        label.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "connected users", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Tahoma", 0, 9))); // NOI18N

        textfield_IP_4.setBackground(new java.awt.Color(240, 240, 240));
        textfield_IP_4.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textfield_IP_4.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        textfield_IP_4.setFocusable(false);

        textfield_IP_2.setBackground(new java.awt.Color(240, 240, 240));
        textfield_IP_2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textfield_IP_2.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        textfield_IP_2.setFocusable(false);

        jLabel7.setText(".");

        jLabel6.setText(".");

        jLabel8.setText(".");

        textfield_IP_1.setBackground(new java.awt.Color(240, 240, 240));
        textfield_IP_1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textfield_IP_1.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        textfield_IP_1.setFocusable(false);

        textfield_IP_3.setBackground(new java.awt.Color(240, 240, 240));
        textfield_IP_3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textfield_IP_3.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        textfield_IP_3.setFocusable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                        .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textfield_IP_1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel8)
                        .addGap(2, 2, 2)
                        .addComponent(textfield_IP_2, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel6)
                        .addGap(2, 2, 2)
                        .addComponent(textfield_IP_3, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(jLabel7)
                        .addGap(2, 2, 2)
                        .addComponent(textfield_IP_4, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textfield_users, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(button_users, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(button_default_port, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(textfield_port, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(button_start, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(button_connect, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(button_dispose, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label)
                            .addComponent(jLabel4)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textfield_IP_1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(textfield_IP_2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(textfield_IP_3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(textfield_IP_4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_default_port, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textfield_port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_users, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textfield_users, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button_dispose, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_connect, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button_start, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button_default_portActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_default_portActionPerformed
        textfield_port.setText("1234");
}//GEN-LAST:event_button_default_portActionPerformed

    private void button_usersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_usersActionPerformed
        textfield_users.setText("unlimited");
    }//GEN-LAST:event_button_usersActionPerformed

    private void button_startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_startActionPerformed
        if (maiden){
            int max = 0;
            boolean portIsNumber = true;
            try{
                Integer.parseInt(textfield_port.getText());
            }
            catch (Exception ex){
                portIsNumber = false;
            }

            boolean usersIsValid = true;
            try{
                max = Integer.parseInt(textfield_users.getText());
            }
            catch (Exception ex){
                usersIsValid = false;
            }
            if (textfield_users.getText().equals("unlimited")){
                usersIsValid = true;
                max = -1;
            }

            if (!portIsNumber){
                textfield_port.setBackground(LIGHT_RED);
            }
            else {
                textfield_port.setBackground(Color.WHITE);
            }
            if (!usersIsValid){
                textfield_users.setBackground(LIGHT_RED);
            }
            else {
                textfield_users.setBackground(Color.WHITE);
            }

            if (portIsNumber && usersIsValid){
                maiden = false;
                button_connect.setEnabled(true);
                try {
                    server = new Server(Integer.parseInt(textfield_port.getText()), max);
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(null, "Connection failed...", "Error", JOptionPane.ERROR_MESSAGE);
                    System.exit(0);
                }
                player_counter.start();

            }
        }
    }//GEN-LAST:event_button_startActionPerformed

    private void button_connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_connectActionPerformed
        button_dispose.setEnabled(true);
        /*button_connect.setEnabled(false);
        server.startPeerToPeer();*/
    }//GEN-LAST:event_button_connectActionPerformed

    private void button_disposeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_disposeActionPerformed
        System.exit(0);
    }//GEN-LAST:event_button_disposeActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ServerInitFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_connect;
    private javax.swing.JButton button_default_port;
    private javax.swing.JButton button_dispose;
    private javax.swing.JButton button_start;
    private javax.swing.JButton button_users;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel label;
    private javax.swing.JTextField textfield_IP_1;
    private javax.swing.JTextField textfield_IP_2;
    private javax.swing.JTextField textfield_IP_3;
    private javax.swing.JTextField textfield_IP_4;
    private javax.swing.JTextField textfield_port;
    private javax.swing.JTextField textfield_users;
    // End of variables declaration//GEN-END:variables

}
